﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using SilverlightCommands;
using System.ServiceModel;
using KMT_Pro.Web;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;

namespace KMT_Pro.ViewModels.ClientViewModels
{
    public class ClientEditUserViewModel : MessageSendableViewModel
    {
        #region Constants and Fields

        CustomerContext _context;

        //콤보박스 로딩용
        private ObservableCollection<ComboBoxCode> companyID_Type;
        private List<ComboBoxCode> ComboList = new List<ComboBoxCode>();
        public ComboBoxCode selectedCompanyID;

        //구조체용
        public string companyID = string.Empty;
        public string companyName = string.Empty;
        public string tel = string.Empty;
        public string phone = string.Empty;
        public string fax = string.Empty;
        public string email = string.Empty;
        public string position = string.Empty;
        public string etc = string.Empty;
        public string iU_info = string.Empty;
        public string idx = string.Empty;
        public string clientName = string.Empty;
        public string gubun;

        #endregion

        #region Constructors and Destructors

        public ClientEditUserViewModel()
        {
            //수정버튼 커맨드
            this.editClick = new RelayCommand((p) =>
                                                {
                                                    try
                                                    {
                                                        if (IU_info == "Insert")
                                                            this.Idx = "0";

                                                        this.IsBusy = true;

                                                        _context = new CustomerContext();

                                                        var q = _context.Load(_context.ClientUserMaster_InsertQuery(this.Idx
                                                                                                                  , this.SelectedCompanyID.Name
                                                                                                                  , this.ClientName
                                                                                                                  , this.Position
                                                                                                                  , this.Tel
                                                                                                                  , this.Phone
                                                                                                                  , this.Fax
                                                                                                                  , this.Email
                                                                                                                  , DateTime.Now.ToString()
                                                                                                                  , this.Etc
                                                                                                                  , this.IU_info));
                                                        q.Completed += EditComplete; //비동기 완료 이벤트 등록
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        this.IsBusy = false;
                                                        ChildWindow errorWin = new ErrorWindow(ex);
                                                        errorWin.Show();
                                                    }
                                                });

            //콤보박스 로딩
            try
            {
                _context = new CustomerContext();
                var q = _context.Load(_context.GetClientPerson_ComboQuery());

                q.Completed += new EventHandler(ComboQueryComplete);
            }
            catch (Exception)
            { }
        }

        #endregion

        #region Properties
        
        // ------------------------ 속성 : Start -----------------------------------
        /// <summary>
        /// 입력 출력 구분 바인딩
        /// </summary>
        public string IU_info
        {
            set
            {
                this.iU_info = value;
                this.OnPropertyChanged("IU_info");
            }
            get
            {
                return iU_info;
            }
        }

        /// <summary>
        /// 회사코드 콤보박스 바인딩
        /// </summary>
        public ObservableCollection<ComboBoxCode> CompanyID_Type
        {
            set
            {
                this.companyID_Type = value;
                this.OnPropertyChanged("CompanyID_Type");
            }
            get
            {
                return companyID_Type;
            }
        }

        /// <summary>
        /// 콤보박스 선택 바인딩
        /// </summary>
        public ComboBoxCode SelectedCompanyID
        {
            set
            {
                this.selectedCompanyID = value;
                this.OnPropertyChanged("SelectedCompanyID");
                this.CompanyName = selectedCompanyID.Name;
            }
            get
            {
                return selectedCompanyID;
            }
        }

        /// <summary>
        /// 회사명 바인딩
        /// </summary>
        public string CompanyName
        {
            set
            {
                this.companyName = value;
                this.OnPropertyChanged("CompanyName");
            }
            get
            {
                return companyName;
            }
        }

        /// <summary>
        /// 회사 전화번호 바인딩
        /// </summary>
        public string Tel
        {
            set
            {
                this.tel = value;
                this.OnPropertyChanged("Tel");
            }
            get
            {
                return tel;
            }
        }

        /// <summary>
        /// 핸드폰 바인딩
        /// </summary>
        public string Phone
        {
            set
            {
                this.phone = value;
                this.OnPropertyChanged("Phone");
            }
            get
            {
                return phone;
            }
        }

        /// <summary>
        /// 팩스 바인딩
        /// </summary>
        public string Fax
        {
            set
            {
                this.fax = value;
                this.OnPropertyChanged("Fax");
            }
            get
            {
                return fax;
            }
        }

        /// <summary>
        /// 핸드폰 바인딩
        /// </summary>
        public string Email
        {
            set
            {
                this.email = value;
                this.OnPropertyChanged("Email");
            }
            get
            {
                return email;
            }
        }

        /// <summary>
        /// 직책 바인딩
        /// </summary>
        public string Position
        {
            set
            {
                this.position = value;
                this.OnPropertyChanged("Position");
            }
            get
            {
                return position;
            }
        }

        /// <summary>
        /// 기타사항 바인딩
        /// </summary>
        public string Etc
        {
            set
            {
                this.etc = value;
                this.OnPropertyChanged("Etc");
            }
            get
            {
                return etc;
            }
        }

        /// <summary>
        /// 인덱스 바인딩
        /// </summary>
        public string Idx
        {
            set
            {
                this.idx = value;
                this.OnPropertyChanged("Idx");
            }
            get
            {
                return idx;
            }
        }

        /// <summary>
        /// 이름 바인딩
        /// </summary>
        public string ClientName
        {
            set
            {
                this.clientName = value;
                this.OnPropertyChanged("ClientName");
            }
            get
            {
                return clientName;
            }
        }
        // ------------------------ 속성 : End -------------------------------------

        //------------------------- 버튼 : Start -----------------------------------

        public ICommand editClick { get; set; }

        //------------------------- 버튼 : End -------------------------------------

        #endregion

        #region Event

        public void ComboQueryComplete(object sender, EventArgs e)
        {
            try
            {
                LoadOperation lo = sender as LoadOperation;

                foreach (ComboBoxCode GAP in lo.AllEntities)
                {
                    ComboList.Add(GAP);
                }

                this.CompanyID_Type = new ObservableCollection<ComboBoxCode>(this.ComboList);
            }
            catch (Exception)
            { }
        }

        public void EditComplete(object sender, EventArgs e)
        {
            LoadOperation lo = sender as LoadOperation;
            try
            {
                foreach (ResultMessage GAP in lo.AllEntities)
                {
                    MessageBox.Show(GAP.ExceptionMessage);
                }
                this.IsBusy = false;
            }
            catch (Exception)
            { this.IsBusy = false; }
        }

        #endregion
    }
}
